package com.tanhua.server.service;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.UserFrozen;
import com.tanhua.domain.vo.CommentVO;
import com.tanhua.domain.vo.ErrorResult;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.dubbo.api.UserFrozenApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {

    @Reference
    private CommentApi commentApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private PublishApi publishApi;
    @Reference
    private UserFrozenApi userFrozenApi;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private MovementMQService mqService;

    /**
     * 查询某个动态文字评论列表
     */
    public ResponseEntity findCommentList(String publishId, int page, int pagesize) {
        PageResult pageResult = commentApi.findCommentListByPublishId(page, pagesize, new ObjectId(publishId));
        List<Comment> items = pageResult.getItems();
        //转换vo
        List<CommentVO> voList = new ArrayList<>();
        for (Comment comment : items) {
            CommentVO vo = new CommentVO();
            //转换userInfo到vo
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            BeanUtils.copyProperties(userInfo, vo);
            //转换comment
            BeanUtils.copyProperties(comment, vo);
            vo.setId(comment.getId().toHexString());
            vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy-MM-dd HH:mm"));
            vo.setHasLiked(redisTemplate.hasKey("publish_like_" + UserHolder.getUser().getId() + "_" + comment.getPublishId().toHexString()) ? 1 : 0);
            voList.add(vo);
        }
        pageResult.setItems(voList);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 提交动态评论
     */
    public ResponseEntity saveComment(String publishId, String content) {
        User user = UserHolder.getUser();
        //查询冻结状态
        UserFrozen userFrozen = userFrozenApi.findUserFrozenByUserId(user.getId());
        if (userFrozen != null && userFrozen.getFreezingRange() == 2) {
            return ResponseEntity.status(500).body(ErrorResult.commentFrozen(userFrozen.getOutTime()));
        }
        //封装Comment
        Comment comment = new Comment();
        comment.setUserId(user.getId());
        comment.setCommentType(2);
        comment.setPublishId(new ObjectId(publishId));
        comment.setContent(content);
        comment.setPubType(1);
        comment.setLikeCount(0);

        //设置被评论的用户
        Publish publish = publishApi.findById(new ObjectId(publishId));
        comment.setPublishUserId(publish.getUserId());

        Integer count = commentApi.saveComment(comment);

        mqService.sendMsgWithComment(publishId);
        return ResponseEntity.ok(count);
    }

}
